package com.ddwl.order.config;

import com.ddwl.common.constant.RabbitMqConstants;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 类RabbitMqConfig.java的实现描述：TODO
 *
 * @author zach
 * @Date :2020-06-04 11:12.
 */
@Configuration
public class RabbitMqConfig {

    /**
     *
     *  订单超时交换器
     * @return the exchange
     */
    @Bean("deadLetterExchange")
    public Exchange deadLetterExchange() {
        return ExchangeBuilder.directExchange(RabbitMqConstants.ORDER_TIMEOUT_DL_EXCHANGE).durable(true).build();
    }

    /**
     * 分消交换机
     * @return
     */
    @Bean("distExchange")
    public Exchange distExchange() {
        return ExchangeBuilder.directExchange(RabbitMqConstants.DIST_EXCHANGE).durable(true).build();
    }

    /**
     * 订单分消利润计算队列
     * @return
     */
    @Bean("orderCalculateQueue")
    public Queue orderCalculateQueue() {
        return QueueBuilder.durable(RabbitMqConstants.DIST_ORDER_CALCULATE_QUEUE).build();
    }

    /**
     * 绑定 订单分消利润计算队列
     * @return
     */
    @Bean
    public Binding orderCalculateBinding() {
        return new Binding(RabbitMqConstants.DIST_ORDER_CALCULATE_QUEUE, Binding.DestinationType.QUEUE, RabbitMqConstants.DIST_EXCHANGE, RabbitMqConstants.DIST_ORDER_CALCULATE_QUEUE, null);
    }


    /**
     * 订单转发队列
     *
     * @return the queue
     */
    @Bean("redirectQueue")
    public Queue redirectQueue() {
        return QueueBuilder.durable(RabbitMqConstants.ORDER_TIMEOUT_REDIRECT_QUEUE).build();
    }

    /**
     * 转发队列绑定交换器
     *
     * @return the binding
     */
    @Bean
    public Binding redirectBinding() {
        return new Binding(RabbitMqConstants.ORDER_TIMEOUT_REDIRECT_QUEUE, Binding.DestinationType.QUEUE, RabbitMqConstants.ORDER_TIMEOUT_DL_EXCHANGE, RabbitMqConstants.ORDER_TIMEOUT_REDIRECT_QUEUE, null);
    }


    /**
     * 死信队列绑定交换器
     *
     * @return the binding
     */
    @Bean
    public Binding deadLetterBinding() {
        return new Binding(RabbitMqConstants.ORDER_TIMEOUT_DL_QUEUE, Binding.DestinationType.QUEUE, RabbitMqConstants.ORDER_TIMEOUT_DL_EXCHANGE, RabbitMqConstants.ORDER_TIMEOUT_DL_QUEUE, null);
    }



    /**
     * 订单死信队列.
     *
     * @return the queue
     */
    @Bean("deadLetterQueue")
    public Queue deadLetterQueue() {
        Map<String, Object> args = new HashMap<>(3);
        //设置转交换器
        args.put("x-dead-letter-exchange", RabbitMqConstants.ORDER_TIMEOUT_DL_EXCHANGE);
        //设置转发交换器key
        args.put("x-dead-letter-routing-key", RabbitMqConstants.ORDER_TIMEOUT_REDIRECT_QUEUE);
        //设置过期时间 单位毫秒
        //args.put("x-message-ttl", express*60*1000);
        return QueueBuilder.durable(RabbitMqConstants.ORDER_TIMEOUT_DL_QUEUE).withArguments(args).build();
    }
}
